<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\grid\CheckboxColumn;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\DocumentSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '文档列表';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="document-index">

    <!--<h1><?= Html::encode($this->title) ?></h1>-->
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('创建文档', ['create'], ['class' => 'btn btn-success']) ?>
        &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
        <?= Html::a("批量删除", "javascript:void(0);", ["class" => "btn btn-success gridview"]) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],

            [
                "class" => "yii\grid\CheckboxColumn",
                "name" => "id",
            ],
            
            
            [
                'attribute'=>'id',
                'contentOptions' =>['width'=>'30'],
            ],
            [
                'attribute'=>'uid',
                'value'=>'uid0.username',
            ],
            //'name',
            [
                'attribute' =>'title',
                'contentOptions'=>['width'=>'350px','overflow'=>'scroll'],
            ],
            [
                'attribute'=>'category_id',
                'value' => 'category0.title',
                'contentOptions' =>['width'=>'80','overflow'=>'hidden',],
            ],
            
            //'group_id',
            //'description',
            //'root',
           //'pid',
            //'model_id',
            //'type',
            //'position',
            //'link_id',
            //'cover_id',
            //'display',
            //'deadline',
            //'attach',
            'view',
            //'comment',
            //'extend',
            //'level',
            //'create_time',
            
            [
            'attribute' => 'create_time',
            'format' => ['date', 'php:Y-m-d'],
              //  'sort'=>'desc',
             ],
            [
            'attribute' => 'update_time',
             //'value' => '',
            'format' => ['date', 'php:Y-m-d'],
              //  'sort'=>'desc',
             ],
            
            [
                 'attribute'=>'status',
                 'value'=> function($model){
                    return $model->status == '1'?"开启":"关闭";
                 },
                 
                
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                //'label' => '操作',
                'buttons' =>[
                    
                     'delete' => function ($url, $model, $key) {
                            return Html::a('<i class="glyphicon glyphicon-trash"></i>', yii\helpers\Url::toRoute(["/document/toggle-trash",'id'=>$key,'tag'=>'delete']), [
                                'title' => '删除',
                                'class' => '',
                                //'onclick'=>'ajaxDelete("'.yii\helpers\Url::toRoute("/document/delete-toggle-trash&id").'","'.$key.'")',
                             
                            ]); 
                        },
                   ]
                
           ],
           
        ],
                                                
//        "options" => [
//            // 设置ID　为后面 jquery 调用  如果你没有设置 哪么你也可以用 #w0 来代码 #grid
//            //"id" => "grid"
//        ],
    ]);
 
$batchUrl = yii\helpers\Url::toRoute("/document/batch-delete");
 $this->registerJs("
$(document).on('click', '.gridview', function () {
    var keys = $('#w0').yiiGridView('getSelectedRows');
    console.log(keys);
    var keyStr = '';
    for(var i=0;i<keys.length;i++){
        keyStr += keys[i] + ',';
    }
    
    $.ajax({
        url:'$batchUrl',
        type:'POST',
        data:{'ids':keyStr,'tag':'trash'},
    })
});
");                       
                        
                        
 ?>
</div>
